package com.qing.design.patterns.flyweight.example;

import com.qing.design.patterns.flyweight.example.forest.Forest;

import java.awt.*;

public class Demo {
	static int CANVAS_SIZE = 500;
	static int TREES_TO_DRAW = 100000;
	static int TREE_TYPES = 2;

	public static void main(String[] args) {
		Forest forest = new Forest();
		for (int i = 0; i < Math.floor(TREES_TO_DRAW / TREE_TYPES); i++) {
			forest.plantTree(random(0, CANVAS_SIZE), random(0, CANVAS_SIZE),
					"夏橡树", Color.GREEN, "Oak texture stub");
			forest.plantTree(random(0, CANVAS_SIZE), random(0, CANVAS_SIZE),
					"秋橡树", Color.ORANGE, "Autumn Oak texture stub");
		}
		forest.setSize(CANVAS_SIZE, CANVAS_SIZE);
		forest.setVisible(true);

		System.out.println(TREES_TO_DRAW + " 绘制的树");
		System.out.println("---------------------");
		System.out.println("内存使用:");
		System.out.println("树大小（8字节） * " + TREES_TO_DRAW);
		System.out.println("+ TreeTypes size (~30 bytes) * " + TREE_TYPES + "");
		System.out.println("---------------------");
		System.out.println("Total: " + ((TREES_TO_DRAW * 8 + TREE_TYPES * 30) / 1024 / 1024) +
				"MB (instead of " + ((TREES_TO_DRAW * 38) / 1024 / 1024) + "MB)");
	}

	private static int random(int min, int max) {
		return min + (int) (Math.random() * ((max - min) + 1));
	}
}
